<?php

class SlidesAction extends Action {

    //查看幻灯片
    function index() {
        $sli = M("slides");
        import('ORG.Util.Page'); // 导入分页类
        $count = $sli->where("")->order("sort asc , ftime desc")->count(); // 查询满足要求的总记录数
        $Page = new Page($count, 5);
        $show = $Page->show(); // 分页显示输出
        $list = $sli->where("")->order("sort asc , ftime desc")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $show); // 赋值分页输出
        $this->assign("list", $list);
        $this->display();
    }

    //添加幻灯片
    function add() {
        if ($_POST) {
            $sli = M("slides");
            $str = $this->_param("str");
            $sort = $this->_param("sort");
			$url = $this->_param("url");
            import("ORG.Net.UploadFile");
            $upload = new UploadFile(); // 实例化上传类
            $upload->maxSize = 3145728; // 设置附件上传大小
            $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg', 'zip'); // 设置附件上传类型
            $upload->savePath = './Public/Uploads/'; // 设置附件上传目录
            if (!$upload->upload()) {
                // 上传错误提示错误信息
//            $this->error($upload->getErrorMsg());
            } else {
                // 上传成功 获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }
            $data['str'] = $str;
            $data['sort'] = $sort;
			$data['url'] = $url;
            $data['ftime'] = date("y-m-d h:i:s");
            $data['pic'] = $info[0]['savename'];
            $li = $sli->add($data);
            if ($li) {
                $this->success("添加成功", "__URL__/index");
            }
        } else {
            $this->display();
        }
    }

    //编辑幻灯片
    function edit($p = "") {
        $sli = M("slides");
        $id = $this->_param("id");
        $p = $this->_param("p");
		$url = $this->_param("url");
        if (!$_POST) {
            $list = $sli->where("id = $id")->select();
            $this->assign("list", $list);
            $this->display();
        } else {
            $str = $this->_param("str");

            $sort = $this->_param("sort");
            import("ORG.Net.UploadFile");
            $upload = new UploadFile(); // 实例化上传类
            $upload->maxSize = 3145728; // 设置附件上传大小
            $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg', 'zip'); // 设置附件上传类型
            $upload->savePath = './Public/Uploads/'; // 设置附件上传目录
            if (!$upload->upload()) {
                // 上传错误提示错误信息
//            $this->error($upload->getErrorMsg());
            } else {
                // 上传成功 获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }
            if ($info[0]['savename'] == null && $info[0]['savename'] == '') {
                $info[0]['savename'] = $p;
            }
            $data['str'] = $str;
			$data['url'] = $url;
            $data['sort'] = $sort;
            $data['xtime'] = date("y-m-d h:i:s");
            $data['pic'] = $info[0]['savename'];
            $li = $sli->where("id = $id")->save($data);
            if ($li) {
                $this->success("编辑成功", "__URL__/index");
            }
        }
    }

    function del() {
        $id = $this->_param(id);
        $del = M("slides");
        $del->where("id = $id")->delete();
        if ($del) {
            $this->redirect("/Slides", 0);
        }
    }

}

